package com.laxcen.toydemo;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.intelligenttoy.BLE.ETag;

import util.BackgroundMusic;
import util.DefaultHandler;
import util.DelayHandler;

public class GameStepSelectActivity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    HomeMenuButtonControl btn_game_step1, btn_game_step2, btn_game_step3, btn_game_step4, btn_game_step5, btn_game_step6, btn_home;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_step_select);
        mGameState.start();

        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);

        btn_game_step1 = findViewById(R.id.btn_game_step1);
        btn_game_step1.setOnClickListener(this);
        btn_game_step2 = findViewById(R.id.btn_game_step2);
        btn_game_step2.setOnClickListener(this);
        btn_game_step3 = findViewById(R.id.btn_game_step3);
        btn_game_step3.setOnClickListener(this);
        btn_game_step4 = findViewById(R.id.btn_game_step4);
        btn_game_step4.setOnClickListener(this);
        btn_game_step5 = findViewById(R.id.btn_game_step5);
        btn_game_step5.setOnClickListener(this);
        btn_game_step6 = findViewById(R.id.btn_game_step6);
        btn_game_step6.setOnClickListener(this);
    }

    private void startRepeatPromptGbSound() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStepSelect) {
                    BackgroundMusic.instance(GameStepSelectActivity.this).play(R.raw.guide_repeat_prompt, false);
                }
            }
        }, 2000);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_game_step1) {
            hitStep(btn_game_step1, GameStep1Activity.class);
        } else if (v.getId() == R.id.btn_game_step2) {
            hitStep(btn_game_step2, GameStep2Activity.class);
        } else if (v.getId() == R.id.btn_game_step3) {
            hitStep(btn_game_step3, GameStep3Activity.class);
        } else if (v.getId() == R.id.btn_game_step4) {
            hitStep(btn_game_step4, GameStep4Activity.class);
        } else if (v.getId() == R.id.btn_game_step5) {
            hitStep(btn_game_step5, GameStep5Activity.class);
        } else if (v.getId() == R.id.btn_game_step6) {
            hitStep(btn_game_step6, GameStep6Activity.class);
        } else if (v.getId() == R.id.btn_home) {
            goback();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        BackgroundMusic.instance(this).stop();
        isWaitTag = false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.GameStepSelect);
        startRepeatPromptGbSound();
        restartTagCheck();
    }


    private boolean isWaitTag = true;

    private void restartTagCheck() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    /* 7,8,9 = 1,2,3 */
    private void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._1)) {
            hitStep(btn_game_step1, GameStep1Activity.class);
        } else if (checkOption(currentTags, ETag._2)) {
            hitStep(btn_game_step2, GameStep2Activity.class);
        } else if (checkOption(currentTags, ETag._3)) {
            hitStep(btn_game_step3, GameStep3Activity.class);
        } else if (checkOption(currentTags, ETag._4)) {
            hitStep(btn_game_step4, GameStep4Activity.class);
        } else if (checkOption(currentTags, ETag._5)) {
            hitStep(btn_game_step5, GameStep5Activity.class);
        } else if (checkOption(currentTags, ETag._6)) {
            hitStep(btn_game_step6, GameStep6Activity.class);
        } else if (checkOption(currentTags, ETag._GOBACK)) {
            goback();
        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }

    private void goback() {
        isWaitTag = false;
        BackgroundMusic.instance(this).stop();
        startActivityAndFinish(GameActivity.class);
    }

    private void hitStep(HomeMenuButtonControl btn, Class c) {
        isWaitTag = false;
        btn.shake();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                startActivity(c);
            }
        }, 1000);
    }
}
